package com.an.blog.strategy.context;

import com.an.blog.strategy.SocialLoginStrategy;
import com.an.blog.web.domain.BlogBindBo;
import com.an.common.core.domain.entity.SysUser;
import com.an.common.enums.SocialLoginType;
import lombok.AllArgsConstructor;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthUser;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;


/**
 * 社会登录回电策略上下文
 *
 * @author: Ann
 * @createDate: 2023/09/01 14:48:12
 * @version: 1.0.0
 */
@Service
@AllArgsConstructor
public class SocialLoginStrategyContext {

    private Map<String, SocialLoginStrategy> socialLoginStrategyMap;


    /**
     * 执行回调策略
     *
     * @param source          源
     * @param response        响应
     * @param callback        回调
     * @param servletResponse servlet响应
     * @throws IOException ioException
     * @author Ann
     * @date 2024/07/08
     */
    public void executeCallBackStrategy(String source, AuthResponse<AuthUser> response,
                                        AuthCallback callback, HttpServletResponse servletResponse) throws IOException {
        socialLoginStrategyMap.get(SocialLoginType.getStrategyBySource(source))
                .socialLoginCallBack(response,callback, servletResponse);
    }

    /**
     * 执行登录绑定社交用户
     *
     * @param blogBindBo 博客绑定参数
     * @param sysUser    系统用户
     * @author Ann
     * @date 2024/07/08
     */
    public void executeLoginBindSocialUser(BlogBindBo blogBindBo, SysUser sysUser) {
        socialLoginStrategyMap.get(SocialLoginType.getStrategyBySource(blogBindBo.getSource()))
            .loginBindSocialUser(blogBindBo,sysUser);
    }

    /**
     * 执行注册绑定社交用户
     *
     * @param blogBindBo 博客绑定参数
     * @param sysUser    系统用户
     * @return {@link AuthUser }
     * @author Ann
     * @date 2024/07/08
     */
    public AuthUser executeRegisterBindSocialUser(BlogBindBo blogBindBo, SysUser sysUser) {
       return socialLoginStrategyMap.get(SocialLoginType.getStrategyBySource(blogBindBo.getSource()))
            .registerBindSocialUser(blogBindBo,sysUser);
    }
}
